
uart = UART(4, 9600)


def send_text(text):
    while 1 :
        uart.write(text)
        if uart.any() :
            reply = uart.read(32)
            if reply == text :
                print(text)
                return (1)

def receive_text():
    while 1 :
        if uart.any():
            text = uart.read(32)
            reply = text
            uart.write(reply)
            print(text)
            return (text)


# 前面5路循迹传感器
FrontSensorPins = ['G6', 'G7', 'G8', 'C6', 'C7']
FrontSensor = [Pin(Pin(pin), Pin.IN) for pin in FrontSensorPins]
def FrontSensorCheck():
    return [FrontSensor[p].value() for p in range(len(FrontSensorPins))]

# 后面5路循迹传感器
BackSensorPins = ['A14','A13','A8','C9','C8']
BackSensor = [Pin(Pin(pin), Pin.IN) for pin in BackSensorPins]
def BackSensorCheck():
    return [BackSensor[p].value() for p in range(len(BackSensorPins))]

# 左面5路循迹传感器
LeftSensorPins = ['A15', 'C10', 'C11', 'C12', 'D6']
LeftSensor = [Pin(Pin(pin), Pin.IN) for pin in LeftSensorPins]
def LeftSensorCheck():
    return [LeftSensor[p].value() for p in range(len(LeftSensorPins))]
def FrontCheckRun(a):#只用前循迹
    if a==[1,1,0,1,1] or a==[1,0,0,0,1]:
        return 'g'
    elif a==[1,0,1,1,1] or a==[1,0,0,1,1]:
        return 'm'
    elif  a==[0,1,1,1,1] or a==[0,0,1,1,1] or a==[0,0,0,1,1]:
        return 'm'
    elif a==[1,1,1,0,1] or a==[1,1,0,0,1]:
        return 'n'
    elif a==[1,1,1,1,0] or a==[1,1,1,0,0] or a==[1,1,0,0,0]:
        return 'n'

def FrontBackCheckRun1(front,back):
    '''
    先同时判断前后两个循迹模块，在判断前循迹模块
    '''

    if front==[1,1,1,1,1]:
        return 'g'
    try:#尝试判断前后循迹模块都有检测到黑线
        a=front.index(0)
        b=back.index(0)
        if a-b==0:#黑线在同一直线
            if a==2:
                lrg='g'
            elif a<2:
                lrg='l'
            elif a>2:
                lrg='r'
        elif a-b<0:#黑线左向45°
            #return 'm'#左转
            if a==2:
                lrg='g'
            elif a<2:
                lrg='m'#左转
            elif a>2:
                lrg='r'#右走
        elif a-b>0:#黑线右向45°
            #return 'n'#右转
            if a==2:
                lrg='g'
            elif a<2:
                lrg='r'#右走
            elif a>2:
                lrg='n'#右转
        return lrg
    except Exception as e:
        '''
        报错的话就是其中一个模块脱离黑线，没有检测到0，
        这里默认算它是后循迹模块脱离了黑线,以只有前循迹模块来循迹，直至后循迹模块出现信号。
        '''
        print(e)
        f=front.index(0)
        if f==2:
            return 'g'
        elif f<2:
            return 'm'#左转
        elif f>2:
            return 'n'#右转
    else:
        return 'g'#其他情况先不管了，直走

def FrontBackCheckRun2(front,back):
    '''
    先判断前循迹模块，后判断后循迹模块
    '''
    try:#前循迹模块检测到黑线
        a=front.index(0)
        #b=back.index(0)
        if a==2:#黑线在前循迹模块2位置
            try:#结合后循迹模块判断运动
                #lrg=BackIndex(back.index(0))
                if ind==2:#前2,后2
                    lrg='g'#直走
                elif ind>2:#前2,后>2
                    lrg='m'#左转
                elif ind<2:#前2,后<2
                    lrg='n'#右转
            except ValueError as e:
                print(e)
                lrg='g'
        elif a<0:#黑线在前循迹模块<2位置
            try:
                if back.index(0)<=a:
                    lrg='r'#右走
                elif back.index(0)>a:
                    lrg='n'#右转
            except ValueError as e:
                print(e)
                lrg='m'#左转
        elif a>0:#黑线在前循迹模块>2位置
            try:
                if back.index(0)>=a:
                    lrg='r'#右走
                elif back.index(0)<a:
                    lrg='n'#右转
            except ValueError as e:
                print(e)
                lrg='n'#右转
        return lrg
    except ValueError as e:
        print(e)
        return 'g'#其他情况先不管了，直走
def GoCheckFBLSensor():#前进状态下各循迹模块的信号值
    return [FrontSensorCheck(),BackSensorCheck(),LeftSensorCheck()]

def SlowStop(Left):#通过左面循迹模块判断是否减速或停止，停止即到达物料区
    if Left==[1,1,1,1,0] or Left==[1,1,1,0,0] or Left==[1,1,0,0,0]:
        return 's'#Slow
    elif Left==[1,0,0,0,0] or Left==[0,0,0,0,0]:
        return 'c'
    else:
        return 'f'



def BackCheckRun(a):#只用后循迹
    if a==[1,1,0,1,1] or a==[1,0,0,0,1]:
        return 'g'
    elif a==[1,0,1,1,1] or a==[1,0,0,1,1]:
        return 'n'
    elif  a==[0,1,1,1,1] or a==[0,0,1,1,1] or a==[0,0,0,1,1]:
        return 'n'
    elif a==[1,1,1,0,1] or a==[1,1,0,0,1]:
        return 'm'
    elif a==[1,1,1,1,0] or a==[1,1,1,0,0] or a==[1,1,0,0,0]:
        return 'm'
def BackCheckFBLSensor():#后退状态下各循迹模块的信号值
    return [BackSensorCheck().reverse() ,FrontSensorCheck().reverse() ,LeftSensorCheck().reverse() ]